package com.wang.bootbuilder.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.bootbuilder.entity.param.UserParam;
import com.wang.bootbuilder.entity.pojo.Role;
import com.wang.bootbuilder.entity.pojo.User;
import com.wang.bootbuilder.mapper.RoleMapper;
import com.wang.bootbuilder.mapper.UserMapper;
import com.wang.bootbuilder.tools.JsonResult;
import com.wang.bootbuilder.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    // 用户Mapper
    @Autowired
    UserMapper userMapper;

    // 角色Mapper
    @Autowired
    RoleMapper roleMapper;

    /*
    添加用户
     */
    public JsonResult addUser(UserParam param) {

        // 判断用户名是否已经存在
        Map<String, Object> selectUserMap = new HashMap<>();
        selectUserMap.put("username", param.getUsername());
        List<User> userList = userMapper.selectByMap(selectUserMap);
        if (!CollectionUtils.isEmpty(userList)) {
            return JsonResult.error().message("此用户已存在!");
        }

        // 登录用户
        User insertUserEntity = new User();
        insertUserEntity.setUsername(param.getUsername());
        insertUserEntity.setPassword(param.getPassword());
        insertUserEntity.setPhone(param.getPhone());
        insertUserEntity.setEmail(param.getEmail());
        insertUserEntity.setRoleId(param.getRoleId());
        userMapper.insert(insertUserEntity);
        return JsonResult.ok();
    }

    /*
    更新用户
     */
    public JsonResult updateUser(UserParam param) {

        // 判断将要更改的用户名是否已经存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", param.getUsername()).ne("id", param.getId());
        List<User> userList = userMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(userList)) {
            return JsonResult.error().message("此用户已存在");
        }

        // 乐观锁检查,并进行更新
        User updateUser = userMapper.selectById(param.getId());
        updateUser.setUsername(param.getUsername());
        updateUser.setPassword(param.getPassword());
        updateUser.setPhone(param.getPhone());
        updateUser.setEmail(param.getEmail());
        updateUser.setRoleId(param.getRoleId());
        updateUser.setCreateBy(null);
        updateUser.setUpdateBy(null);
        updateUser.setCreateTime(null);
        updateUser.setUpdateTime(null);
        int i = userMapper.updateById(updateUser);
        if (i == 0) {
            return JsonResult.error().message("该数据已被其他用户修改,请重新检索后再次修改");
        }
        // 检索更新之后的用户数据
        User resultUser = userMapper.selectById(param.getId());
        return JsonResult.ok().data("user", resultUser);
    }

    /*
    获取所有用户列表
     */
    public JsonResult getUsers() {
        // 获取所有用户
        List<User> usersList = userMapper.selectList(null);
        List<Role> rolesList = roleMapper.selectList(null);
        Map<String, Object> usersMap = new HashMap<>();
        usersMap.put("users", usersList);
        usersMap.put("roles", rolesList);
        return JsonResult.ok().data(usersMap);
    }

    /*
    删除指定用户
     */
    public JsonResult deleteUser(Long userId) {
        userMapper.deleteById(userId);
        return JsonResult.ok();
    }

}
